//
//  PersonalInformationViewController.m
//  HeartTravel
//
//  Created by lanou on 15/10/26.
//  Copyright © 2015年 陈文杰. All rights reserved.
//

#import "PersonalInformationViewController.h"
#import "HTSettingTableViewCell.h"
#import "HTSingleTon.h"
#import "SDImageCache.h"
#import "HTClearCachFile.h"
@interface PersonalInformationViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UIView *nightview;
@property (nonatomic, strong) UIView *pushview;
@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, strong) UILabel *cachelabel;
@property (nonatomic, strong) NSArray *files;
@property (nonatomic, strong) NSFileManager *fileManager;
@property (nonatomic, strong) HTClearCachFile *clearCachFile;
@end

@implementation PersonalInformationViewController

- (HTClearCachFile *)clearCachFile
{
    if (!_clearCachFile) {
        _clearCachFile = [[HTClearCachFile alloc]init];
    }
    return _clearCachFile;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //夜间模式view
    _nightview = [[UIView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    _nightview.backgroundColor = [UIColor blackColor];
    _nightview.userInteractionEnabled = NO;
    _nightview.alpha = 0;
    [[[UIApplication sharedApplication] keyWindow] addSubview:_nightview];
    UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(0, 70, SCREEN_WIDTH, SCREEN_HEIGHT)];
    imageview.image = [UIImage imageNamed:@"Blue背景"];
    [self.view addSubview:imageview];
//    self.view.backgroundColor = [UIColor colorWithRed:0.296 green:0.932 blue:1.000 alpha:1.000];
    
    //头像信息等
    [self touxiang];
   //返回按钮
    [self ruternbutton];
    
    
    UITableView *tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 250, SCREEN_WIDTH, SCREEN_HEIGHT) style:(UITableViewStylePlain)];
    tableview.dataSource = self;
    tableview.delegate = self;
    //去掉表尾
    tableview.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:tableview];
    
    [tableview registerClass:[HTSettingTableViewCell class] forCellReuseIdentifier:@"reuser"];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    _cachelabel.text = [NSString stringWithFormat:@"%.1fM",[self.clearCachFile shouCachs]] ;
}

- (void)doreturn
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HTSettingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuser" forIndexPath:indexPath];
    
    if (indexPath.row == 0) {
        cell.cacheLabel.text = @"夜间模式";
        UISwitch *changeColor = [[UISwitch alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 100, 15, 50, 30)];
        [changeColor addTarget:self action:@selector(changeBackColor:) forControlEvents:(UIControlEventValueChanged)];
        [cell addSubview:changeColor];
    }else if (indexPath.row == 1)
    {
        _cachelabel = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 100, 15, 50, 30)];
        _cachelabel.text = [NSString stringWithFormat:@"%.1fM",[self.clearCachFile shouCachs]] ;
        [cell addSubview:_cachelabel];
        cell.cacheLabel.text = @"清空缓存";
    }else{
        cell.cacheLabel.text = @"当前版本";
    }
    return cell;
}


- (void)changeBackColor:(UISwitch *)change
{
    HTSingleTon *sinleton = [HTSingleTon shareInstance];
    if (sinleton.isOn == NO) {
        _nightview.alpha = 0.5;
        sinleton.isOn = YES;
    }else{
        sinleton.isOn = NO;
        _nightview.alpha = 0;
        
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

- (void)touxiang
{
    _imageView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 100, 80, 80)];
    _imageView.backgroundColor = [UIColor whiteColor];
    _imageView.layer.cornerRadius = 40;
    _imageView.layer.masksToBounds = YES;
      HTSingleTon *singleTon = [HTSingleTon shareInstance];
    _imageView.image = singleTon.iocnPicture.image;
    if (singleTon.iocnPicture.image == nil) {
        _imageView.image = [UIImage imageNamed:@"注册头像占位.jpg"];
    }
    [self.view addSubview:_imageView];
    
    _name = [[UILabel alloc]initWithFrame:CGRectMake(110, 115, 100, 50)];
    _name.text = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
    [self.view addSubview:_name];

}

//返回的button
- (void)ruternbutton
{
    UIButton *returnButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    returnButton.frame = CGRectMake(10, 15, 20, 20);
    [returnButton setImage:[UIImage imageNamed:@"return"] forState:(UIControlStateNormal)];
    
    [returnButton addTarget:self action:@selector(doreturn2) forControlEvents:(UIControlEventTouchUpInside)];
    [self.topView addSubview:returnButton];

}

- (void)doreturn2
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 1) {
     
        [self.clearCachFile clearCadchs];
       _cachelabel.text = [NSString stringWithFormat:@"%.1fM",[self.clearCachFile shouCachs]] ;
    }else if (indexPath.row == 2){
      
        UIAlertView *alerView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"当前以是最新版本" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alerView show];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
